package com.study.main;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import com.study.writeandreadobj.GuestLoggingInfo;
import com.study.writeandreadobj.Logginginfo;

public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		logingInfo();
		
//		guestInfo();
	}

	private static void guestInfo() {
		final String filename = "guestinfo.out";
		
		GuestLoggingInfo old_guest = new GuestLoggingInfo();
		System.out.println("old_guest = " + old_guest.toString() + 
				"\n -------------------------------------");
		
		writeAndReadObj(filename, old_guest);
	}

	private static void logingInfo() {
		final String filename = "loginfo.out";
		
		Logginginfo old_loginfo = new Logginginfo("Mike", "MECHANICS");
		System.out.println("old_loginfo = " + old_loginfo.toString() + 
				"\n -------------------------------------");
		
		writeAndReadObj(filename, old_loginfo);
	}

	private static <T> void writeAndReadObj(final String filename,
			T old_loginfo) {
		try {
			ObjectOutputStream o = new ObjectOutputStream(
					new FileOutputStream(filename));
			o.writeObject(old_loginfo);
			o.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		try {
			ObjectInputStream in = new ObjectInputStream(
					new FileInputStream(filename));
			
			@SuppressWarnings("unchecked")
			T new_loginfo = (T) in.readObject();// 不需要受检
			System.out.println("new_loginfo = " + new_loginfo);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

}
